package com.googlecode.dtools.typeinfo.work.impl;

import com.googlecode.dtools.typeinfo.beans.DmType;
import com.googlecode.dtools.typeinfo.work.FilesWorker;
import com.googlecode.dtools.typeinfo.work.HierarchyWorker;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @author A_Reshetnikov
 * @since Date: 31.05.12 Time: 17:49
 */
public class HierarchyWorkerImpl implements HierarchyWorker {
    private FilesWorker filesWorker;

    public void work() {
        List<DmType> list = filesWorker.readDmTypesFromFileSystem();
        for (DmType dmType : list) {
            String s = map2WikiString(dmType);
            System.out.println(s);
        }
    }

    public String calculate2BrowseTree() {
        String res = "";
        List<DmType> list = filesWorker.readDmTypesFromFileSystem();
        for (DmType dmType : list) {
            String superName = dmType.getSuperName();
            if ("".equals(dmType.getSuperName())) superName = "null";
            if (dmType.getName() != null) {
                if (dmType.getName().startsWith("kc")
                        || dmType.getName().equals("dm_document")
                        || dmType.getName().equals("dm_sysobject")
                        || dmType.getName().equals("dm_folder")
                        || dmType.getName().equals("dm_user"))
                    res += superName + " -> " + dmType.getName() + "\n";
            }
        }
        return res;
    }

    private String map2WikiString(DmType dmType) {
        String res = "";
        if (dmType.getHierarchyLevel() != null) {
            for (int i = 0; i < dmType.getHierarchyLevel(); i++) {
                res += "  ";
            }
        }
        res += " * ";
        if (dmType.getCount() != null && dmType.getCount() == 0)
            res += "<del>[[" + dmType.getName() + "]]</del>";
        else res += "[[" + dmType.getName() + "]]";
        return res;
    }

    public void setFilesWorker(FilesWorker filesWorker) {
        this.filesWorker = filesWorker;
    }
}
